/**
 * 
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import dao.*;
import entity.*;
import utils.DBHelper;


/**
 * @author Chen Lei
 *
 */
public class ArticleDAO {
	private ArticleDAO() {
	}

	public static ArticleDAO getInstance() {
		return new ArticleDAO();
	}
	/**
	 * 得到系统帖子总数
	 *
	 */
	public int getTotal() {
		int total = 0;
		try {

			Connection c = DBHelper.getInstance().getConnection();

			Statement s = c.createStatement();

			String sql = "select count(*) from article";

			ResultSet rs = s.executeQuery(sql);
			while (rs.next()) {
				total = rs.getInt(1);
			}

			System.out.println("total:" + total);

			DBHelper.closeConnection(c, s, rs);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return total;
	}
	/**
	 * 添加一个帖子连带帖子的内容
	 * @param String article_title 文章标题
	 * @param int user_id 用户id
	 * @param int theme_id  主题id
	 * 
	 */
	public int add(String article_title,int user_id,int theme_id,String article_content){
		try {
			int article_id = addArticle(article_title,user_id,theme_id);
			System.out.print("我想要article——id"+article_id);
			Content content = new Content(article_id,article_content);
			ContentDAO.getInstance().add(content);
			return article_id;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	/**私有函数！！！！！！！！！
	 * 添加一个帖子，只有基本信息，没有内容
	 * @param String article_title 文章标题
	 * @param int user_id 用户id
	 * @param int theme_id  主题id
	 * 
	 */
	private int addArticle(String article_title,int user_id,int theme_id){
		try {
			Connection c = DBHelper.getInstance().getConnection();
			System.out.print("打印user的id"+user_id);
			String sql = "insert into article values(null,?,?,?,0,0,0,?,?,0,0)";
			PreparedStatement ps = c.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, article_title);
			ps.setInt(2,user_id);
			ps.setInt(3, theme_id);
			ps.setDate(4,new java.sql.Date(new Date().getTime()));
			ps.setDate(5,new java.sql.Date(new Date().getTime()));
			
			ps.execute();

			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next()) {
				int content_id =  rs.getInt(1);
				System.out.println(content_id);
				DBHelper.closeConnection(c, ps, null);
				return content_id;
			}

			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	/**
	 * 通过id得到一条帖子
	 *
	 */
	public Article get(int article_id) {
		Article article = null;
		try {
			
			Connection c = DBHelper.getInstance().getConnection();

			Statement s = c.createStatement();

			String sql = "select * from article where article_id = " + article_id;

			ResultSet rs = s.executeQuery(sql);

			while (rs.next()) {

				article = new Article();
				
				String article_title = rs.getString("article_title");
				int user_id = rs.getInt("user_id");
				int theme_id = rs.getInt("theme_id");
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				int article_user_istop = rs.getInt("article_user_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");
				
				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_user_istop(article_user_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
			}

			DBHelper.closeConnection(c, s, rs);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return article;
	}
	/**
	 * 得到用户个人主页置顶帖子列表
	 *
	 */
	public List<Article> list_user_top(int user_id) {
		return list_user_top(0, Short.MAX_VALUE, user_id);
	}
	/**
	 * 得到从start开始count个用户个人主页的置顶帖子列表
	 *
	 */
	public List<Article> list_user_top(int start, int count, int user_id) {
		List<Article> articles = new ArrayList<Article>();

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "select * from article where article_user_istop = 1 and user_id = "+ user_id +" order by article_lastchangetime DESC limit ?,? ";

			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Article article = new Article();
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				
				int theme_id = rs.getInt("theme_id");
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				int article_user_istop = rs.getInt("article_user_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");
				
				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_user_istop(article_user_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);
			}
			DBHelper.closeConnection(c, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 得到用户个人主页非置顶帖子列表
	 *
	 */
	public List<Article> list_use(int user_id) {
		return list_user(0, Short.MAX_VALUE, user_id);
	}
	/**
	 * 得到从start开始count个用户个人主页的非置顶帖子列表
	 *
	 */
	public List<Article> list_user(int start, int count, int user_id) {
		List<Article> articles = new ArrayList<Article>();

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "select * from article where article_user_istop = 0 and user_id = "+ user_id +" order by article_lastchangetime DESC limit ?,? ";

			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Article article = new Article();
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				
				int theme_id = rs.getInt("theme_id");
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				int article_user_istop = rs.getInt("article_user_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");
				
				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_user_istop(article_user_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);
			}
			DBHelper.closeConnection(c, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 得到置顶帖子列表
	 *
	 */
	public List<Article> list_top() {
		return list_top(0, Short.MAX_VALUE);
	}
	/**
	 * 得到从start开始count个置顶帖子列表
	 *
	 */
	public List<Article> list_top(int start, int count) {
		List<Article> articles = new ArrayList<Article>();

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "select * from article where article_istop = 1 order by article_lastchangetime DESC limit ?,? ";

			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Article article = new Article();
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				int user_id = rs.getInt("user_id");
				int theme_id = rs.getInt("theme_id");
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				int article_user_istop = rs.getInt("article_user_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");
				
				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_user_istop(article_user_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);
			}
			DBHelper.closeConnection(c, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 得到所有帖子列表
	 *
	 */
	public List<Article> list() {
		return list(0, Short.MAX_VALUE);
	}
	/**
	 * 得到从start开始count个所有帖子列表
	 *
	 */
	public List<Article> list(int start, int count) {
		List<Article> articles = new ArrayList<Article>();

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "select * from article order by article_lastchangetime DESC limit ?,? ";

			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Article article = new Article();
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				int user_id = rs.getInt("user_id");
				int theme_id = rs.getInt("theme_id");
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				int article_user_istop = rs.getInt("article_user_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");
				
				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_user_istop(article_user_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);
			}
			DBHelper.closeConnection(c, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 得到非置顶帖子列表
	 *
	 */
	public List<Article> list_untop() {
		return list_untop(0, Short.MAX_VALUE);
	}
	/**
	 * 得到从start开始count个非置顶帖子列表
	 *
	 */
	public List<Article> list_untop(int start, int count) {
		List<Article> articles = new ArrayList<Article>();

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "select * from article where article_istop = 0 order by article_lastchangetime DESC limit ?,? ";

			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Article article = new Article();
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				int user_id = rs.getInt("user_id");
				int theme_id = rs.getInt("theme_id");
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				int article_user_istop = rs.getInt("article_user_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");
				
				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_user_istop(article_user_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);
			}
			DBHelper.closeConnection(c, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 得到该主题的置顶帖子列表
	 *
	 */
	public List<Article> list_top(int theme_id) {
		return list_top(0, Short.MAX_VALUE, theme_id);
	}
	/**
	 * 得到从start开始count个该主题的置顶帖子列表
	 *
	 */
	public List<Article> list_top(int start, int count, int theme_id) {
		List<Article> articles = new ArrayList<Article>();

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "select * from article where theme_id = "+ theme_id +" and article_istop = 1 order by article_lastchangetime DESC limit ?,? ";

			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Article article = new Article();
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				int user_id = rs.getInt("user_id");
				
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				int article_user_istop = rs.getInt("article_user_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");
				
				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_user_istop(article_user_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);
			}
			DBHelper.closeConnection(c, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 得到该主题的非置顶帖子列表
	 *
	 */
	public List<Article> list(int theme_id) {
		return list(0, Short.MAX_VALUE, theme_id);
	}
	/**
	 * 得到从start开始count个该主题的非置顶帖子列表
	 *
	 */
	public List<Article> list(int start, int count, int theme_id) {
		List<Article> articles = new ArrayList<Article>();

		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "select * from article where theme_id = "+ theme_id +" and article_istop = 0 order by article_lastchangetime DESC limit ?,? ";

			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, count);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Article article = new Article();
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				int user_id = rs.getInt("user_id");
				
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				int article_user_istop = rs.getInt("article_user_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");
				
				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_user_istop(article_user_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);
			}
			DBHelper.closeConnection(c, ps, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 删除帖子
	 *
	 */
	public void delete(int article_id) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			Statement s = c.createStatement();

			String sql = "delete from article where article_id = " + article_id;

			s.execute(sql);

			DBHelper.closeConnection(c, s, null);
			ContentDAO.getInstance().deleteByAid(article_id);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 管理员置顶帖子
	 *
	 */
	public void topArticle(Article article) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set article_istop = ? where article_id = " + article.getArticle_id() ;
			
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, 1);
			
			ps.execute();
			
			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 用户置顶帖子
	 *
	 */
	public void topArticle_user(Article article) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set article_user_istop = ? where article_id = " + article.getArticle_id() ;
			
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, 1);
			
			ps.execute();
			
			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 更新帖子的主题为"其它"，当某主题被删掉的时候
	 *
	 */
	public void modifyTheme_toDefault(int theme_id) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set theme_id = ? where theme_id = " + theme_id ;
			
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, 1);
			
			ps.execute();
			
			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 更新帖子的用户为"已注销用户"，当某主题被删掉的时候
	 *
	 */
	public void modifyUser_toDefault(int user_id) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set user_id = ? where user_id = " + user_id ;
			
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, 1000);
			
			ps.execute();
			
			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 管理员取消置顶帖子
	 *
	 */
	public void cancelTopArticle(Article article) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set article_istop = ? where article_id = " + article.getArticle_id() ;
			
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, 0);
			
			ps.execute();
			
			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 用户取消置顶帖子
	 *
	 */
	public void cancelTopArticle_user(Article article) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set article_user_istop = ? where article_id = " + article.getArticle_id() ;
			
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, 0);
			
			ps.execute();
			
			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/**
	 * 在首页搜索帖子
	 *
	 */
	public List<Article> searchArticleIndex(String keyword) {

		List<Article> articles = new ArrayList<Article>();
		try {

			Connection c = DBHelper.getInstance().getConnection();

			Statement s = c.createStatement();

			String sql = "select * from article where article_title like '%" + keyword + "%'";

			ResultSet rs = s.executeQuery(sql);

			while (rs.next()) {
				
				Article article = new Article();
				
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				int user_id = rs.getInt("user_id");
				int theme_id = rs.getInt("theme_id");
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");

				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);

			}

			DBHelper.closeConnection(c, s, rs);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 在个人主页搜索帖子
	 *
	 */
	public List<Article> searchArticleHomepage(String keyword, int user_id) {

		List<Article> articles = new ArrayList<Article>();
		try {

			Connection c = DBHelper.getInstance().getConnection();

			Statement s = c.createStatement();

			String sql = "select * from article where user_id ="+ user_id +" and article_title like '%" + keyword + "%'";

			ResultSet rs = s.executeQuery(sql);

			while (rs.next()) {
				
				Article article = new Article();
				
				int article_id = rs.getInt("article_id");
				String article_title = rs.getString("article_title");
				int theme_id = rs.getInt("theme_id");
				int article_click_num = rs.getInt("article_click_num");
				int article_comment_num = rs.getInt("article_comment_num");
				int article_istop = rs.getInt("article_istop");
				Date article_createtime = rs.getDate("article_createtime");
				Date article_lastchangetime = rs.getDate("article_lastchangetime");
				int article_zan_num = rs.getInt("article_zan_num");

				article.setArticle_id(article_id);
				article.setArticle_title(article_title);
				article.setUser_id(user_id);
				article.setTheme_id(theme_id);
				article.setArticle_click_num(article_click_num);
				article.setArticle_comment_num(article_comment_num);
				article.setArticle_istop(article_istop);
				article.setArticle_createtime(article_createtime);
				article.setArticle_lastchangetime(article_lastchangetime);
				article.setArticle_zan_num(article_zan_num);
				
				articles.add(article);

			}

			DBHelper.closeConnection(c, s, rs);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	/**
	 * 更新浏览量
	 *
	 */
	public void modifyClickTime(int article_id) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set article_click_num =article_click_num+1  where article_id = " + article_id ;
			
			PreparedStatement ps = c.prepareStatement(sql);			
			ps.execute();
			
			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public void update(Article article,String content) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set article_title= ?,article_lastchangetime=?,theme_id=? where article_id = " + article.getArticle_id();
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setString(1, article.getArticle_title());
			ps.setDate(2,new java.sql.Date(new Date().getTime()));
			ps.setInt(3,article.getTheme_id());
			ContentDAO.getInstance().update(article.getArticle_id(), content);
			ps.execute();
		
			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/****
	 * 
	 *更新帖子信息
	 */
	public void update(Article article) {
		try {

			Connection c = DBHelper.getInstance().getConnection();

			String sql = "update article set article_title= ?, user_id = ?,theme_id=?,article_istop=?,article_user_istop=?,article_createtime=?,article_lastchangetime=?,article_comment_num=? where article_id = ?";
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setString(1, article.getArticle_title());
			ps.setInt(2, article.getUser_id());
			ps.setInt(3, article.getTheme_id());
			ps.setInt(4, article.getArticle_istop());
			ps.setInt(5, article.getArticle_user_istop());
			
		    ps.setDate(6, (java.sql.Date) article.getArticle_createtime());
		    ps.setDate(7, new java.sql.Date(new Date().getTime()));
		   // ps.setDate(7, (java.sql.Date) new Date(System.currentTimeMillis()));
		    ps.setInt(8,article.getArticle_comment_num());
			ps.setInt(9, article.getArticle_id());
			

			ps.execute();

			DBHelper.closeConnection(c, ps, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
